package com.xujianlong.day06;

import com.xujianlong.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Flink01_KeyedState_ValueState {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> dataStreamSource = env.socketTextStream("hadoop102", 9999);

        SingleOutputStreamOperator<WaterSensor> map = dataStreamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String s) throws Exception {
                String[] split = s.split(",");

                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        KeyedStream<WaterSensor, String> keyedStream = map.keyBy(r -> r.getId());
        //5.检测传感器的水位线值，如果连续的两个水位线差值超过10，就输出报警。
        keyedStream.process(new KeyedProcessFunction<String, WaterSensor, String>() {
            private ValueState<Integer> valueState;

            @Override
            public void open(Configuration parameters) throws Exception {
                valueState=getRuntimeContext().getState(new ValueStateDescriptor<Integer>("value_state",Integer.class));
            }

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                if(null==valueState.value()){
                    valueState.update(value.getVc());
                }
                if(Math.abs(value.getVc()-valueState.value())>10){
                    out.collect("水位线超过十报警！！！");
                }
                valueState.update(value.getVc());
            }
        }).print();
        
        //mmmmmm
        keyedStream.process(new KeyedProcessFunction<String, WaterSensor, String>() {
            private ValueState<Integer> valueState;
            @Override
            public void open(Configuration parameters) throws Exception {
                valueState=getRuntimeContext().getState(new ValueStateDescriptor<Integer>("value_state",Integer.class));
            }

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                if(null==valueState.value()){
                    valueState.update(value.getVc());
                }
                if(Math.abs(value.getVc()-valueState.value())>10){
                    out.collect("ddd");
                }
                valueState.update(value.getVc());
            }
        });
        //mmmmm

        env.execute();
    }
}
